﻿using AutoMapper;
using SIG.Service;
using SIG.Service.Store;
using SIG.ViewModel.Front.Store;
using SIG.Model.Store;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SIG.WebMVC.Controllers
{
    public class ShoppingCartController : BaseController
    {
      
        private readonly ICartService _cartService;
        private readonly IOrderService _orderService;
        private readonly IGoodsService _goodsService;
        private readonly IRecipientInfoService _recipientInfoService;
        private readonly IMapper _mapper;
        public ShoppingCartController(ICartService cartService, IOrderService orderService, IGoodsService goodsService, IRecipientInfoService recipientInfoService, IMapper mapper)
        {
            _cartService = cartService;
            _orderService = orderService;
            _goodsService = goodsService;
            _recipientInfoService = recipientInfoService;
            _mapper = mapper;
        }

        // GET: ShoppingCart
        public ActionResult Index()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext, _cartService, _orderService);

            // Set up our ViewModel
            var viewModel = new ShoppingCartVM
            {
                CartItems = cart.GetCartItems(),
                CartTotal = cart.GetTotal()
            };
            // Return the view
            return View(viewModel);
        }


        //
        // GET: /Store/AddToCart/5
        public ActionResult AddToCart(int id)
        {
            // Retrieve the album from the database
            var addedGoods = _goodsService.GetById(id);

            // Add it to the shopping cart
            var cart = ShoppingCart.GetCart(this.HttpContext, _cartService, _orderService);
            cart.AddToCart(addedGoods);
     
            return RedirectToAction("Index");
        }
        //
        // AJAX: /ShoppingCart/RemoveFromCart/5
        [HttpPost]
        public ActionResult RemoveFromCart(int id)
        {
            // Remove the item from the cart
            var cart = ShoppingCart.GetCart(this.HttpContext,_cartService,_orderService);

            // Get the name of the album to display confirmation
            var goodsId = _cartService.GetById(id).GoodsId;
            string goodsName = _goodsService.GetById(goodsId).Name;            

            // Remove from cart
            int itemCount = cart.RemoveFromCart(id);

            // Display the confirmation message
            var results = new ShoppingCartRemoveVM
            {
                Message = Server.HtmlEncode(goodsName) +
                    " has been removed from your shopping cart.",
                CartTotal = cart.GetTotal(),
                CartCount = cart.GetCount(),
                ItemCount = itemCount,
                DeleteId = id
            };
            return Json(results);
        }
        //
        // GET: /ShoppingCart/CartSummary
        [ChildActionOnly]
        public ActionResult CartSummary()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext, _cartService, _orderService);

            ViewData["CartCount"] = cart.GetCount();
            return PartialView("_CartSummary");
        }

        [Authorize]
        public ActionResult SetAddress()
        {
            return View();
        }
        [Authorize]
        [HttpPost]
        public ActionResult Payment(int addressId)
        {
            var cart = ShoppingCart.GetCart(this.HttpContext, _cartService, _orderService);
            var viewModel = new ShoppingCartVM
            {
                CartItems = cart.GetCartItems(),
                CartTotal = cart.GetTotal()
            };

            if (cart.GetCount() == 0){
                TempData["Error"] = "您的购物车是空的，请先选购商品！";
            }

            
            var address = _recipientInfoService.GetById(addressId);
            if (address == null)
            {
                TempData["Error"] = "请填写收件地址！";
                return View();
            }
            ViewBag.Address = address;
            return View(viewModel);
        }


    }
}